package com.example.springsecuritydemo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;
    @Autowired
    private DataSource dataSource;
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        // 赋值数据源
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        // 自动创建表 , 第一次执行会创建，以后要执行就要删除掉！
        jdbcTokenRepository.setDataSource(dataSource);
        //jdbcTokenRepository.setCreateTableOnStartup(true);

        return jdbcTokenRepository;
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 退出操作
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/hello").permitAll();
        // 跳转没有权限页面
        http.exceptionHandling().accessDeniedPage("/unauth.html");

        http.formLogin()  // 自定义编写的登录页面
           .loginPage("/login.html")  //  登录页面
           .loginProcessingUrl("/user/login")  // 登录访问路径  访问的controller
           .defaultSuccessUrl("/success.html").permitAll()  // 登录成功之后,跳转路径
           .and().authorizeRequests()
                 .antMatchers("/","/test/hello","/user/login").permitAll()  // 设置哪些路径可以直接访问不需要认证
                //.antMatchers("/test/index").hasAnyAuthority("admins")   // 当前用户只有admins权限可以访问
                //.antMatchers("/test/index").hasAnyAuthority("role","admins")  // 前用户可以后多个权限可以访问
                .antMatchers("/test/index").hasRole("sale")
           .anyRequest().authenticated() // 所有请求都不需要验证
                .and().rememberMe().tokenRepository(persistentTokenRepository())  //设置记住我功能
                .tokenValiditySeconds(60)   // 设置有效时长
                .userDetailsService(userDetailsService);
           //.and().csrf().disable() ; // 关闭csrf防护
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
}
